package com.ssy.lingxi.search.controller.mobile.channel;

import cn.hutool.core.bean.BeanUtil;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.search.api.model.request.channel.ChannelCommodityCollectRequest;
import com.ssy.lingxi.search.api.model.response.CollectResponse;
import com.ssy.lingxi.search.api.model.response.CommoditySearchResponse;
import com.ssy.lingxi.search.entity.Commodity;
import com.ssy.lingxi.search.entity.CommodityShop;
import com.ssy.lingxi.search.entity.channel.ChannelCommodityMember;
import com.ssy.lingxi.search.entity.channel.CommodityCollectChannel;
import com.ssy.lingxi.search.model.response.CommodityCollectResponse;
import com.ssy.lingxi.search.repository.CommodityRepository;
import com.ssy.lingxi.search.service.ICommodityCollectChannelService;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @menu: APP-渠道商城--商品收藏管理
 * 商品收藏管理
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/9/2
 */
@RestController
@RequestMapping("/search/mobile/shop/commodityCollectChannel")
public class MobileCommodityCollectChannelController extends BaseController {

    @Resource
    private ICommodityCollectChannelService commodityCollectChannelService;

    @Resource
    private CommodityRepository commodityRepository;

    /**
     * 查询商品是否已经收藏
     * @param channelMemberId 渠道会员id
     * @param channelMemberRoleId 渠道会员角色id
     * @param channelCommodityId 商品id
     */
    @RequestMapping(value = "/getCommodityCollect", method = RequestMethod.GET)
    public Wrapper<CollectResponse> getCommodityCollect(@RequestParam("channelMemberId") Long channelMemberId, @RequestParam("channelMemberRoleId") Long channelMemberRoleId, @RequestParam("channelCommodityId") Long channelCommodityId) {
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityCollectChannelService.getCommodityCollect(channelMemberId, channelMemberRoleId, channelCommodityId, sysUser, shopId));
    }

    /**
     * 查询商品收藏列表
     * @param pageVO 分页实体
     * @param channelMemberId 渠道供应商id
     * @return
     */
    @RequestMapping(value = "/getCommodityCollectList", method = RequestMethod.GET)
    public Wrapper<PageData<CommodityCollectResponse>> getCommodityCollectList(PageVO pageVO, @RequestParam("channelMemberId") Long channelMemberId) {
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = this.getSysUser();

        Page<CommodityCollectChannel> result = commodityCollectChannelService.getCommodityCollectChannelList(sysUser, shopId, channelMemberId, pageVO);
        List<CommodityCollectResponse> resultList = result.stream().map(commodityCollectChannel -> {
            CommodityCollectResponse commodityCollectResponse = new CommodityCollectResponse();
            commodityCollectResponse.setIsPublish(false);
            Long channelCommodityId = commodityCollectChannel.getChannelCommodityId();
            Long commodityCollectId = commodityCollectChannel.getId();
            Long createTime = commodityCollectChannel.getCreateTime();
            commodityCollectResponse.setId(commodityCollectId);
            commodityCollectResponse.setShopId(commodityCollectChannel.getShopId());
            commodityCollectResponse.setCreateTime(createTime);
            commodityCollectResponse.setChannelMemberId(channelMemberId);
            Commodity commodity = commodityRepository.findFirstByMemberListChannelCommodityMemberId(channelCommodityId);
            if(commodity != null){
                CommoditySearchResponse commoditySearchResponse = new CommoditySearchResponse();
                BeanUtil.copyProperties(commodity, commoditySearchResponse);
                commodityCollectResponse.setCommodity(commoditySearchResponse);
                //判断渠道商品是否上架
                List<ChannelCommodityMember> memberList = commodity.getMemberList();
                if(memberList != null && memberList.size() > 0){
                    memberList.forEach(channelCommodityMember -> {
                        Long channelCommodityMemberId = channelCommodityMember.getChannelMemberId();
                        if(channelMemberId != null){
                            if(channelCommodityMemberId.longValue() == channelMemberId.longValue()){
                                List<CommodityShop> shopList = channelCommodityMember.getShopList();
                                if(shopList != null && shopList.size() > 0){
                                    List<Long> shopIdList = shopList.stream().map(CommodityShop::getShopId).collect(Collectors.toList());
                                    if(shopIdList.contains(shopId)){
                                        commodityCollectResponse.setIsPublish(true);
                                    }
                                }
                            }
                        }
                    });
                }
            }else{
                return null;
            }
            return commodityCollectResponse;
        }).filter(Objects::nonNull).collect(Collectors.toList());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 新增商品收藏
     * @param commodityCollectChannelRequest 商品收藏实体
     * @return 商品收藏id
     */
    @RequestMapping(value = "/saveCommodityCollect", method = RequestMethod.POST)
    public Wrapper<Long> saveCommodityCollect(@RequestBody ChannelCommodityCollectRequest commodityCollectChannelRequest){
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityCollectChannelService.saveCommodityCollectChannel(sysUser, shopId, commodityCollectChannelRequest));
    }

    /**
     * 删除商品收藏
     * @param commodityCollectChannelRequest
     * @return
     */
    @RequestMapping(value = "deleteCommodityCollect", method = RequestMethod.POST)
    public Wrapper<String> deleteCommodityCollect(@RequestBody ChannelCommodityCollectRequest commodityCollectChannelRequest){
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityCollectChannelService.deleteCommodityCollectChannel(sysUser, shopId, commodityCollectChannelRequest));
    }

}
